package services.kingdee

import play.api.Logging
import play.api.libs.json.{JsObject, JsValue, Json}

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}

class EmployeeInformation @Inject()(
                                     kingdeePost: KingdeePostV1,
                                     kingdeePostV2: KingdeePostV2
                                   )(implicit ec: ExecutionContext)
  extends Logging {
  private def kdapi(dcid: String) = kingdeePost.setConfig(dcid)

  def save(number: String, name: String, dcid: String ="") = {
    val api = kingdeePost.setConfig(dcid)
    val config = api.kdAppConfig
    val para = Json.obj(
      "IsAutoSubmitAndAudit" -> "true",
      "IsControlPrecision" -> "false",
      "Model" -> Json.obj(
        "FID" -> 0,
        "FName" -> name,
        "FStaffNumber" -> number,
        "FUseOrgId" -> Json.obj("FNumber" -> config.orgNum),
        "FCreateOrgId" -> Json.obj("FNumber" -> config.orgNum),
        "FCreateSaler" -> false,
        "FCreateUser" -> false,
        "FCreateCashier" -> false,
        "FJoinDate" -> "2022-05-24 00:00:00",
        "FSHRMapEntity" -> Json.obj()
      )
    )
    //"formid": "BD_Empinfo"
    api.webApiSave(para)
  }

  def view() = {}

  def list() = {}

  def get_center = kingdeePostV2.get_center

  def get_org = {

    """{
      |    "FormId": "ORG_Organizations",
      |    "FieldKeys": "FOrgID,FName,FNumber,FDocumentStatus,FForbidStatus ",
      |    "FilterString": [],
      |    "OrderString": "",
      |    "TopRowCount": 0,
      |    "StartRow": 0,
      |    "Limit": 100,
      |    "SubSystemId": ""
      |}""".stripMargin

    //      [[1,"礼顿卫生用品（佛山）有限公司","201","C","A"]]
  }

  def sync(accid: String, code: String) = {
    // WY000001:张三
    val n = code.split(":")
    val number = n(0)
    val name = n(1)
    Future(s"$accid($code):Ok; ")
  }

}
